#include "stdafx.h"
#include "HomeManager.h"

HomeManager::HomeManager()
{
}
HomeManager::~HomeManager()
{

}

void HomeManager::loadHomeCSV()
{
	if(m_mapHomeCSV.size() != 0)
		return;
	std::string path = singleton<GameServer>::instance().GetServerPath();
	std::string filename = path + "csv/home_building.csv";
#ifndef _WIN32
	filename = "./csv/home_building.csv";
#endif
	m_csvReader.Load(filename);

	for (Int32 i = 0; i < m_csvReader.GetRowCount(); i++)
	{
		Int32 id = m_csvReader.GetInt(i,"id");
		if(m_mapHomeCSV.find(id) != m_mapHomeCSV.end())
		{
			LOGGER_ERROR("m_mapHomeCSV.find(id) != m_mapHomeCSV.end(),id:%d",id);
			return;
		}

		m_mapHomeCSV[id].csv_id = id;
		HomeCSVInfo& info = m_mapHomeCSV[id];
		
		info.type = m_csvReader.GetInt(i,"type");
		info.name = m_csvReader.GetString(i,"name");
		info.description = m_csvReader.GetString(i,"description");
		info.image_id = m_csvReader.GetInt(i,"image_id");
		info.require_level = m_csvReader.GetInt(i,"require_level");
		info.bustling  = m_csvReader.GetInt(i,"bustling ");
	}
}
HomeCSVInfo* HomeManager::GetHomeCSVInfo(Int32 csv_id)
{
	std::map<Int32,HomeCSVInfo>::iterator iter = m_mapHomeCSV.find(csv_id);
	if (iter == m_mapHomeCSV.end())
	{
		LOGGER_ERROR("HomeManager,iter == m_mapHomeCSV.end(),csv_id:%d",csv_id);
		return NULL;
	}
	return &(iter->second);
}

void HomeManager::loadHomeCateCSV()
{
	if(m_mapHomeCateCSV.size() != 0)
		return;
	std::string path = singleton<GameServer>::instance().GetServerPath();
	std::string filename = path + "csv/home_cate.csv";
#ifndef _WIN32
	filename = "./csv/home_cate.csv";
#endif
	m_csvReaderCate.Load(filename);

	for (Int32 i = 0; i < m_csvReaderCate.GetRowCount(); i++)
	{
		Int32 id = m_csvReaderCate.GetInt(i,"id");
		if(m_mapHomeCateCSV.find(id) != m_mapHomeCateCSV.end())
		{
			LOGGER_ERROR("m_mapHomeCateCSV.find(id) != m_mapHomeCateCSV.end(),id:%d",id);
			return;
		}

		m_mapHomeCateCSV[id].csv_id = id;
		HomeCateCSVInfo& info = m_mapHomeCateCSV[id];
		info.description = m_csvReaderCate.GetString(i,"description");
		info.item_id = m_csvReaderCate.GetInt(i,"item_id");
		info.require_level = m_csvReaderCate.GetInt(i,"require_level");
	}
}
HomeCateCSVInfo* HomeManager::GetHomeCateCSVInfo(Int32 csv_id)
{
	std::map<Int32,HomeCateCSVInfo>::iterator iter = m_mapHomeCateCSV.find(csv_id);
	if (iter == m_mapHomeCateCSV.end())
	{
		LOGGER_ERROR("GetHomeCateCSVInfo,iter == m_mapHomeCateCSV.end(),csv_id:%d",csv_id);
		return NULL;
	}
	return &(iter->second);
}

void HomeManager::loadHomeLandCSV()
{
	if(m_mapHomeLandCSV.size() != 0)
		return;
	std::string path = singleton<GameServer>::instance().GetServerPath();
	std::string filename = path + "csv/home_land.csv";
#ifndef _WIN32
	filename = "./csv/home_land.csv";
#endif
	m_csvReaderLand.Load(filename);

	for (Int32 i = 0; i < m_csvReaderLand.GetRowCount(); i++)
	{
		Int32 id = m_csvReaderLand.GetInt(i,"id");
		if(m_mapHomeLandCSV.find(id) != m_mapHomeLandCSV.end())
		{
			LOGGER_ERROR("m_mapHomeLandCSV.find(id) != m_mapHomeLandCSV.end(),id:%d",id);
			return;
		}

		m_mapHomeLandCSV[id].csv_id = id;
		HomeLandCSVInfo& info = m_mapHomeLandCSV[id];
		info.description = m_csvReaderLand.GetString(i,"description");
		info.item_id = m_csvReaderLand.GetInt(i,"item_id");
		info.require_level = m_csvReaderLand.GetInt(i,"require_level");
	}
}
HomeLandCSVInfo* HomeManager::GetHomeLandCSVInfo(Int32 csv_id)
{
	std::map<Int32,HomeLandCSVInfo>::iterator iter = m_mapHomeLandCSV.find(csv_id);
	if (iter == m_mapHomeLandCSV.end())
	{
		LOGGER_ERROR("GetHomeLandCSVInfo,iter == m_mapHomeLandCSV.end(),csv_id:%d",csv_id);
		return NULL;
	}
	return &(iter->second);
}

void HomeManager::loadHomePetEggCSV()
{
	if(m_mapHomePetEggCSV.size() != 0)
		return;
	std::string path = singleton<GameServer>::instance().GetServerPath();
	std::string filename = path + "csv/home_pet_egg.csv";
#ifndef _WIN32
	filename = "./csv/home_pet_egg.csv";
#endif
	m_csvReaderPetEgg.Load(filename);

	for (Int32 i = 0; i < m_csvReaderPetEgg.GetRowCount(); i++)
	{
		Int32 id = m_csvReaderPetEgg.GetInt(i,"id");
		if(m_mapHomePetEggCSV.find(id) != m_mapHomePetEggCSV.end())
		{
			LOGGER_ERROR("m_mapHomePetEggCSV.find(id) != m_mapHomePetEggCSV.end(),id:%d",id);
			return;
		}

		m_mapHomePetEggCSV[id].csv_id = id;
		HomePetEggCSVInfo& info = m_mapHomePetEggCSV[id];
		info.description = m_csvReaderPetEgg.GetString(i,"description");
		info.item_id = m_csvReaderPetEgg.GetInt(i,"item_id");
		info.require_level = m_csvReaderPetEgg.GetInt(i,"require_level");
		info.price_min = m_csvReaderPetEgg.GetInt(i,"price_min");
		info.price_max = m_csvReaderPetEgg.GetInt(i,"price_max");
	}
}
HomePetEggCSVInfo* HomeManager::GetHomePetEggCSVInfo(Int32 csv_id)
{
	std::map<Int32,HomePetEggCSVInfo>::iterator iter = m_mapHomePetEggCSV.find(csv_id);
	if (iter == m_mapHomePetEggCSV.end())
	{
		LOGGER_ERROR("GetHomePetEggCSVInfo,iter == m_mapHomePetEggCSV.end(),csv_id:%d",csv_id);
		return NULL;
	}
	return &(iter->second);
}
HomePetEggCSVInfo* HomeManager::GetRandomHomePetEggCSVInfo()
{
	Int32 nIndex = rand()%m_mapHomePetEggCSV.size();
	std::map<Int32,HomePetEggCSVInfo>::iterator iter = m_mapHomePetEggCSV.begin();
	std::map<Int32,HomePetEggCSVInfo>::iterator iterend = m_mapHomePetEggCSV.end();
	Int32 nCount = 0;
	for (; iter != iterend; iter++)
	{
		if(nCount == nIndex)
			return &(iter->second);
		nCount++;
	}
	return NULL;
}
HomePetBookCSVInfo* HomeManager::GetRandomHomePetBookCSVInfo()
{
	Int32 nIndex = rand()%m_mapHomePetBookCSV.size();
	std::map<Int32,HomePetBookCSVInfo>::iterator iter = m_mapHomePetBookCSV.begin();
	std::map<Int32,HomePetBookCSVInfo>::iterator iterend = m_mapHomePetBookCSV.end();
	Int32 nCount = 0;
	for (; iter != iterend; iter++)
	{
		if(nCount == nIndex)
			return &(iter->second);
		nCount++;
	}
	return NULL;
}
void HomeManager::loadHomePetBookCSV()
{
	if(m_mapHomePetBookCSV.size() != 0)
		return;
	std::string path = singleton<GameServer>::instance().GetServerPath();
	std::string filename = path + "csv/home_pet_book.csv";
#ifndef _WIN32
	filename = "./csv/home_pet_book.csv";
#endif
	m_csvReaderPetBook.Load(filename);

	for (Int32 i = 0; i < m_csvReaderPetBook.GetRowCount(); i++)
	{
		Int32 id = m_csvReaderPetBook.GetInt(i,"id");
		if(m_mapHomePetBookCSV.find(id) != m_mapHomePetBookCSV.end())
		{
			LOGGER_ERROR("m_mapHomePetBookCSV.find(id) != m_mapHomePetBookCSV.end(),id:%d",id);
			return;
		}

		m_mapHomePetBookCSV[id].csv_id = id;
		HomePetBookCSVInfo& info = m_mapHomePetBookCSV[id];
		info.description = m_csvReaderPetBook.GetString(i,"description");
		info.item_id = m_csvReaderPetBook.GetInt(i,"item_id");
		info.require_level = m_csvReaderPetBook.GetInt(i,"require_level");
		info.price_min = m_csvReaderPetBook.GetInt(i,"price_min");
		info.price_max = m_csvReaderPetBook.GetInt(i,"price_max");
	}
}
HomePetBookCSVInfo* HomeManager::GetHomePetBookCSVInfo(Int32 csv_id)
{
	std::map<Int32,HomePetBookCSVInfo>::iterator iter = m_mapHomePetBookCSV.find(csv_id);
	if (iter == m_mapHomePetBookCSV.end())
	{
		LOGGER_ERROR("GetHomePetBookCSVInfo,iter == m_mapHomePetBookCSV.end(),csv_id:%d",csv_id);
		return NULL;
	}
	return &(iter->second);
}